package com.billsystem.service;

import ch.qos.logback.core.db.dialect.DBUtil;
import cn.hutool.core.bean.BeanUtil;
import com.billsystem.mapper.AccountMapper;
import com.billsystem.mapper.CategoryMapper;
import com.billsystem.mapper.RecordMapper;
import com.billsystem.pojo.Account;
import com.billsystem.pojo.Category;
import com.billsystem.pojo.Record;
import com.billsystem.pojo.vo.RecordVo;
import com.billsystem.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class RecordService {

    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Transactional
    public void add(Record record){
        recordMapper.addRecord(record);
        double money = record.getSpend();
        if (record.getIncomeExpend() == '0') {
            money = -money;
        }
        accountMapper.updateAmount(money, record.getUid(), record.getAid());
    }

    @Transactional
    public void updateRecord(Record record){
        Record oldRecord = recordMapper.queryRecord(record.getId());
        if (oldRecord.getAid() != record.getAid()) {
            double oldMoney = oldRecord.getSpend();
            double money = record.getSpend();
            if (oldRecord.getIncomeExpend() != '0') {
                oldMoney = -oldMoney;
            }
            if (record.getIncomeExpend() == '0') {
                money = -money;
            }
            accountMapper.updateAmount(oldMoney, oldRecord.getUid(), oldRecord.getAid());
            accountMapper.updateAmount(money, oldRecord.getUid(), record.getAid());
            recordMapper.updateRecord(record);
        } else {
            double money = record.getSpend() - oldRecord.getSpend();
            if (record.getIncomeExpend() == '0') {
                money = -money;
            }
            accountMapper.updateAmount(money, oldRecord.getUid(), oldRecord.getAid());
            recordMapper.updateRecord(record);
        }

    }

    @Transactional
    public void deleteRecord(int id){
        Record oldRecord = recordMapper.queryRecord(id);
        double money = oldRecord.getSpend();
        if (oldRecord.getIncomeExpend() != '0') {
            money = -money;
        }
        accountMapper.updateAmount(money, oldRecord.getUid(), oldRecord.getAid());
        recordMapper.deleteRecord(id);
    }

    public Record queryRecord(int id){
        return recordMapper.queryRecord(id);
    }

    public List<RecordVo> queryRecordList(int uid, char incomeExpend){
        List<Record> lists= recordMapper.queryRecordList(uid, incomeExpend);
        List<RecordVo> vos = new ArrayList<>(lists.size());
        for(Record record : lists){
            RecordVo vo =BeanUtil.copyProperties(record, RecordVo.class);
            java.util.Date date = vo.getDate();
            vo.setDate(DateUtil.util2sql(date));
            Category category = categoryMapper.queryCategory(record.getCid());
            Account account = accountMapper.queryAccount(record.getAid());
            vo.setCategoryName(category.getName());
            vo.setAccountName(account.getAccountName());
            vos.add(vo);
        }
        return vos;
    }

    public List<Record> queryRecordToday(Record record){
        return recordMapper.queryRecordToday(record);
    }

    public List<Record> queryRecordThisMonth(Date start, Date end, int uid, char incomeExpend){
        return recordMapper.queryRecordThisMonth(start, end, uid, incomeExpend);
    }

    public int queryCountOfCategory(int cid, int uid){
        return recordMapper.queryCountOfCategory(cid, uid);
    }

    public double queryCountOfSpend(int cid, int uid){
        return recordMapper.queryCountOfSpend(cid, uid);
    }

}
